package top.infopub.mgr.mgc.web;


import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.infopub.generic.GenericController;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.mgc.domain.MsgTemplateDomain;
import top.infopub.mgr.mgc.service.MsgTemplateMainService;
import top.infopub.mgr.mgc.service.MsgTemplateService;
import top.infopub.mgr.util.StreamRenderUtil;
import top.infopub.model.mgc.MsgTemplate;
import top.infopub.model.mgc.MsgTemplateMain;

import com.github.pagehelper.Page;


/**
 * 消息模板配置控制层
 * @author Awoke
 * @version 2018年11月8日
 * @see MsgMgrController
 * @since
 */
@Controller
@RequestMapping("/msgMgr")
public class MsgMgrController extends GenericController {

    @Autowired
    private MsgTemplateMainService templateMainService;

    @Autowired
    private MsgTemplateService templateService;

    /** DEL_FLAG  0：可用  1：删除（不可用） **/
    /**可用 */
    private static final Integer DEL_ENABLE = 0;

    /**已删除*/
    private static final Integer DEL_DISABLE = 1;

    /**
     * 进入消息模板配置
     * @return
     */
    @RequestMapping("/point/list")
    public String list() {
        return "mgc/laymsg-temp-setup-list";
    }

    /**
     * 查询消息主模板
     * @return
     */
    @RequestMapping("/point/query")
    public @ResponseBody RspVo<MsgTemplateMain> queryMain(MsgTemplateMain bean,
                                                               ParamPage paramPage) {
        Page<MsgTemplateMain> page = new Page<MsgTemplateMain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        bean.setDelFlag(DEL_ENABLE);
        Page<MsgTemplateMain> pageList = templateMainService.queryPage(page, bean);
        return RspVo.getSuccessResponse(pageList.getTotal(), pageList.getResult());
    }

    /**
     * 查询消息模板明细
     * @return
     */
    @RequestMapping("/detail/query")
    public @ResponseBody RspVo<MsgTemplateDomain> queryDetail(MsgTemplate bean,
                                                                   ParamPage paramPage) {
        Page<MsgTemplateDomain> page = new Page<MsgTemplateDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        // bean.setDelFlag(DEL_ENABLE);
        Page<MsgTemplateDomain> pageList = templateService.queryPageByDomainWhere(page, bean);
        return RspVo.getSuccessResponse(pageList.getTotal(), pageList.getResult());
    }

    /**
     * 通过ID查询预览消息模板内容
     * @return
     */
    @RequestMapping("/detail/view")
    public void viewTemplateContent(String id, HttpServletResponse response) {
        MsgTemplate msgTemplate = templateService.selectByPrimaryKey(id);
        StreamRenderUtil.render(msgTemplate.getTemplateContent(), response,
            "text/html; charset=utf-8");
    }

    /**
     * 批量删除消息主模板
     * @param checkboxId
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/point/delete")
    public @ResponseBody RspVo deleteMain(String id) {
        MsgTemplateMain model = new MsgTemplateMain();
        model.setId(id);
        model.setDelFlag(DEL_DISABLE);
        templateMainService.updateBySelective(model);
        return RspVo.getSuccessResponse();
    }

    /**
     * 删除/停用 消息模板详情
     * @param checkboxId
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/detail/delete")
    public @ResponseBody RspVo deleteDetail(String id) {
        MsgTemplate model = new MsgTemplate();
        model.setId(id);
        model.setDelFlag(DEL_DISABLE);
        model.setUpdatetime(new Date());
        templateService.updateBySelective(model);
        return RspVo.getSuccessResponse();
    }
    
    /**
     * 启用消息模板详情
     * @param checkboxId
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/detail/enable")
    public @ResponseBody RspVo enableDetail(String id) {
        MsgTemplate model = new MsgTemplate();
        model.setId(id);
        model.setDelFlag(DEL_ENABLE); // 启用
        model.setUpdatetime(new Date());
        templateService.updateBySelective(model);
        return RspVo.getSuccessResponse();
    }

    /**
     * 新增消息主模板
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/point/insert")
    public @ResponseBody RspVo insertMain(MsgTemplateMain bean) {
        if (templateMainService.isCodeExist(bean)) {
            return ErrorCodeEnum.ERROR_COMMON_CODE_UK.getRspVo();
        }
        bean.setDelFlag(DEL_ENABLE);
        bean.setAddtime(new Date());
        templateMainService.insert(bean);
        return RspVo.getSuccessResponse();
    }

    /**
     * 新增消息模板详情
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/detail/insert")
    public @ResponseBody RspVo insertDetail(MsgTemplate bean) {
        bean.setDelFlag(DEL_ENABLE);
        bean.setAddtime(new Date());
        bean.setSendUserCountLimit(0);
        bean.setSendCountLimit(0);
        bean.setIsEnable(1); // 可用
        bean.setIsRealtime(1);// 实时
        templateService.insert(bean);
        return RspVo.getSuccessResponse();
    }

    /**
     * 修改消息主模板
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/point/update")
    public @ResponseBody RspVo updateMain(MsgTemplateMain bean) {
        MsgTemplateMain info = templateMainService.selectByPrimaryKey(bean.getId());
        if (info != null) {
            if (!info.getCode().equals(bean.getCode())) {
                if (templateMainService.isCodeExist(bean)) {
                    return ErrorCodeEnum.ERROR_COMMON_CODE_UK.getRspVo();
                }
            }
        }
        bean.setUpdatetime(new Date());
        templateMainService.updateBySelective(bean);
        return RspVo.getSuccessResponse();
    }

    /**
     * 修改消息模板详情
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/detail/update")
    public @ResponseBody RspVo updateDetail(MsgTemplate bean) {
        bean.setMainId(null);
        bean.setUpdatetime(new Date());
        templateService.updateBySelective(bean);
        return RspVo.getSuccessResponse();
    }

}
